package com.baizhi.travels.contorller;

import com.baizhi.travels.entity.Result;
import com.baizhi.travels.entity.User;
import com.baizhi.travels.service.UserService;
import com.baizhi.travels.utils.CreateImageCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * Description:travels
 * Created by admin  on 2020/5/19  14:35
 */

@RestController
@RequestMapping("user")
@CrossOrigin //允许跨域
@Slf4j  //日志相关
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户登录
     * @param code
     * @param user
     * @param session
     * @return
     */
    @PostMapping("login")
    private Result login(String code, @RequestBody User user,HttpSession session){
        Result result = new Result();
        String sessionCode = (String)session.getAttribute("code");
        session.removeAttribute("code");
        log.info("接受的验证码："+code);
        log.info("实际的验证码："+sessionCode);
        log.info("接受的对象："+user);
        //验证验证码
        if (code != null && (user.getUsername() !=null && user.getPassword() != null)){
            if (code.equalsIgnoreCase(sessionCode)){
                try {
                    User userDB = userService.login(user);
                    //登录成功后保存用户标记
                    session.setAttribute("curr_user",userDB);
                    result.setMsg("登录成功");
                    result.setUserId(userDB.getId());
                } catch (Exception e) {
                    result.setState(false);
                    result.setMsg(e.getMessage());
                }

            }else {
                result.setMsg("验证码不正确");
                result.setState(false);
            }
        }else {
            result.setMsg("验证码或者用户信息不能为空");
            result.setState(false);
        }
        System.out.println(result.getMsg());
        return result;
    }

    /**
     * 用户注册
     * @param code
     * @param user
     * @return
     */
    @PostMapping("register")
    public Result register(String code, @RequestBody User user, HttpSession session){
        Result result = new Result();
        String sessionCode = (String) session.getAttribute("code");
        session.removeAttribute("code");
        //打印出接受的验证码
        log.info("接受的验证码："+code);
        log.info("实际的验证码："+sessionCode);
        log.info("接受的对象："+user);
        //验证验证码
        if (code != null && (user.getPassword() !=null && user.getUsername() !=null)){
           if (code.equalsIgnoreCase(sessionCode)){
               userService.register(user);
               result.setMsg("注册成功");
           }else {
               result.setMsg("验证码不正确");
               result.setState(false);
           }
        }else {
            result.setMsg("用户名信息和验证码不能为空");
            result.setState(false);
        }

        return result;

    }

    /**
     * 生成验证码
     * @param response
     * @param session
     * @throws IOException
     */
    @GetMapping("getImage")
    public void getImage(HttpServletResponse response, HttpSession session) throws IOException {
        CreateImageCode createImageCode = new CreateImageCode();
        //获取验证码
        String securityCode =createImageCode.getCode();
        //验证码存入session
        session.setAttribute("code",securityCode);
        //生成图片
        BufferedImage image = createImageCode.getBuffImg();
        //响应浏览器
        response.setContentType("image/png");
        createImageCode.write(response.getOutputStream());
    }


}
